package com.yutel.system.struts.extjs;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.yutel.system.repository.ExtComm;
import com.yutel.system.repository.ExtPanel;

public class TreePanel extends BasePanel{
	public String make(ExtPanel panel){
		StringBuffer sb=new StringBuffer();
		if(comm(panel)){
			sb.append("P").append(panel.getId()).append("= Ext.extend(Ext.tree.TreePanel, { \n");
			sb.append(" initComponent : function() { \n");
			sb.append(" Ext.apply(this, { \n");
			sb.append("  id:'wyid").append(panel.getId()).append("',");
			sb.append("  frame : true,border:true,");
			sb.append("  iconCls : 'icon-nav', \n");
			sb.append("  minSize : 200,\n");
			sb.append("  maxSize : 250,\n");
			sb.append("  autoScroll : true, \n");
			sb.append("  autoWigth : true, \n");
			sb.append("  containerScroll : true, \n");
			sb.append("  title:'").append(panel.getName()).append("',");
			if(panel.getWidth()!=null && panel.getWidth()>0) {
				sb.append("width : ").append(panel.getWidth()).append(", \n");
			}
			if(panel.getHeight()!=null && panel.getHeight()>0) {
				sb.append("height : ").append(panel.getHeight()).append(", \n");
			}
			if(panel.getRegion()!=null && !"".equals(panel.getRegion())) {
				sb.append("region : '").append(panel.getRegion()).append("', \n");
			}
			sb.append("  rootVisible : true, \n");
			sb.append("  root : new Ext.tree.AsyncTreeNode({ \n");
			sb.append("   id:'0', \n");
			sb.append("   text:'").append(panel.getName()).append("', \n");
			sb.append("   qtip:'").append(panel.getName()).append("', \n");
			sb.append("   expanded : false \n");
			sb.append("  }), \n");
			sb.append("  loader : new TonyTreeLoader({ \n");
			sb.append("   url : 'tempTree.action', \n");
			sb.append("   listeners : { \n");
			sb.append("    'beforeload' : function(treeLoader,node) { \n");
			sb.append("      treeLoader.baseParams['temp.id'] = node.id; \n");
			sb.append("      treeLoader.baseParams['temp.tableName']='").append(panel.getTableName()).append("'; \n");
			sb.append("      treeLoader.baseParams['temp.pidColName']='").append(map.get("pidColName")).append("'; \n");
			sb.append("      treeLoader.baseParams['temp.textColName']='").append(map.get("textColName")).append("'; \n");
			sb.append("    } \n");
			sb.append("   } \n");
			sb.append("  }) \n");
			sb.append("}); \n");
			sb.append("P").append(panel.getId()).append(".superclass.initComponent.call(this);");
			if(map.get("targetCol")!=null) {
				sb.append(" this.addListener('click', function(node) { \n");
				sb.append("   var listP=p").append(panel.getRid()).append(".findById('wyid").append(map.get("targetId")).append("'); \n");
				sb.append("   listP.getStore().baseParams['temp.id'] = node.id; \n");
				sb.append("   listP.getStore().baseParams['temp.panelId']='").append(map.get("targetId")).append("'; \n");
//				sb.append("   listP.getStore().baseParams['temp.tableName']='").append(panel.getTableName()).append("'; \n");
				sb.append("   listP.getStore().baseParams['temp.pidColName']='").append(map.get("targetCol")).append("'; \n");
				sb.append("   listP.getStore().load(); \n");
//				sb.append("   listP.buttonEnable(); \n");
				sb.append(" }); \n");
			}
			sb.append(" } \n");
			sb.append("}); \n");
		}else {
			sb.append("P").append(panel.getId()).append("= Ext.extend(Ext.Panel, { \n");
			sb.append(" initComponent : function() { \n");
			sb.append("Ext.apply(this, {");
			sb.append("id:'wyid").append(panel.getId()).append("',");
			sb.append("title:'页面错误',");
			sb.append("frame : true,border:true,");
			if(panel.getRegion()!=null && !"".equals(panel.getRegion())) {
				sb.append("region:'").append(panel.getRegion()).append("', \n");
			}
			sb.append("html:'请检查面版的如下设置！").append(msg).append("', \n");
			sb.append("}); \n");
			sb.append("P").append(panel.getId()).append(".superclass.initComponent.call(this);");
			sb.append("} \n");
			sb.append("}); \n");
		}
		
		return sb.toString();
	}
	
	private boolean comm(ExtPanel panel){
		Set<ExtComm> set=panel.getComm();
		for(ExtComm ec:set){
			if("PARENTCOL".equalsIgnoreCase(ec.getType())) {
				map.put("pidColName", ec.getColName());
			} else if("TEXTCOL".equalsIgnoreCase(ec.getType())){
				map.put("textColName", ec.getColName());
			} else if("CLICK".equalsIgnoreCase(ec.getType())){
				map.put("rootId", ec.getName());
				map.put("targetId", ec.getTargetId());
				map.put("targetCol", ec.getColName());
			}
		}
		if(map.get("pidColName")==null){
			msg.append("<br>PARENTCOL");
		}
		if(map.get("textColName")==null){
			msg.append("<br>TEXTCOL");
		}
		return msg.length()<1;
	}
	public static void main(String[] args){
		ExtPanel p=new ExtPanel();
		TreePanel ep=new TreePanel();
		System.out.println(ep.make(p));
	}
	
	public TreePanel() {
		map=new HashMap<String,Object>();
	}
	private Map<String,Object> map;
	private StringBuffer msg=new StringBuffer();
	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

}
